package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.queryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private ApplicationContext ctx;
    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    @Transactional(readOnly = true)
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);

    }

    @Override
    @Transactional(readOnly = true)
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public void update(Permission permission) {
     permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public PageResult query(queryObject qo) {
        int totalCount = permissionMapper.selectForCount(qo);

        int totalPage = totalCount % qo.getPageSize() == 0 ?
                totalCount / qo.getPageSize() : totalCount / qo.getPageSize()+1;

        if(qo.getCurrentPage()>totalPage){
            qo.setCurrentPage(totalPage);
        }

        if(totalCount==0){
            return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),totalCount, Collections.emptyList());
        }

        List<Permission> listData = permissionMapper.selectForList(qo);
        return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),totalCount,listData);
    }

    @Override
    public void reload() {
        //获取数据库中的所有权限表达式
        //1.获取所有的controller
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        //2.遍历每个controller，获取里面的每个方法
        Collection<Object> controllers = map.values();
       //controller的字节码对象
        //当前字节码中的方法
        //3.遍历每个方法，获取方法上的RequiredPermission，判断该方法上是否有贴上自定义注解
        for (Object controller:controllers){
            Class clazz = controller.getClass();
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method:methods) {
                //获取方法上的RequiredPermission
                RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                if(annotation!=null){
                    //4.获取注解上的权限名称，以及权限表达式
                    String value = annotation.value();
                    String expression = annotation.expression();
                    //5.封装成权限对象
                    Permission permission = new Permission();
                    permission.setName(value);
                    permission.setExpression(expression);
                    //6.把对象插入到数据库中
                    permissionMapper.insert(permission);
                }
            }
        };
    }

    @Override
    public List<String> selectByElId(Long employeeId) {
        return permissionMapper.selectByElId(employeeId);
    }
}
